<?php 
header("Content-Type:text/html;charset=utf-8");
include 'includes/init.php';
session_start();

$mid=isset($_SESSION['id'])?$_SESSION['id']:'';
$nphone = isset($_SESSION['phone']) ? $_SESSION['phone'] : '';
// echo $nphone;
$time = time();
	if(isset($_GET['usertype']) == 1){
		// 用户所属项目
		$sql = "SELECT DISTINCT
				p.project
			FROM
				t_member AS m,
				t_product AS p,
				t_bid AS b
			WHERE
				(
					p.type = b.btype
					AND p.mifeng = 0
				)
			OR (
				p.mifeng = 1
				AND p.lasttime < ".$time."
				AND p.type = b.btype
			)
			ORDER BY
				p.type ASC,
				b.price ASC ";

		error_log("table2 SQL1: ".$sql);
		$project = $db_Obj ->getall($sql);
	}else{
		// 用户所属项目
		$sql = "SELECT DISTINCT
				p.project
			FROM
				t_member AS m,
				t_product AS p,
				t_bid AS b
			WHERE
				(
					p.mid = '".$mid."'
					AND p.type = b.btype
					AND p.mifeng = 0
				)
			OR (
				p.mifeng = 1
				AND p.lasttime < ".$time."
				AND p.mid = '".$mid."'
				AND p.type = b.btype
			)
			ORDER BY
				p.type ASC,
				b.price ASC ";

        error_log("table2 SQL2: ".$sql);
		$project = $db_Obj ->getall($sql);
	}
	// 项目筛选
	if(!empty($_POST['project'])){
		$pro = empty($_POST['project']) ? '' : $_POST['project'];
			$sql = "SELECT
			m.company_name,
			m.company_data,
			p.nphone,
			p.project,
			p.lasttime,
			p.units,
			p.currency,
			p.number,
			p.material,
			p.purchase,
			p.plant,
			p.extranum,
			p.validtime,
			p.invalidtime,
			b.price,
			b.btype,
			b.bsize,
			b.delivery,
			b.MOQ,
			b.MPQ,
			b.createtime,
			b.rate,
			b.mid
		FROM
			t_member AS m,
			t_product AS p,
			t_bid AS b
		WHERE
			(
				p.mifeng = 0 
				AND p.project = '".$pro."'
				AND b.mid = m.id
				AND p.type = b.btype
			)
		OR (
			p.mifeng = 1
			AND p.lasttime < ".$time."
			AND p.project = '".$pro."'
			AND b.mid = m.id
			AND p.type = b.btype
		)	
		ORDER BY p.type asc, b.price asc
		";

        error_log("table2 SQL3: ".$sql);
		$infoAll = $db_Obj->getall($sql);
	}

	// 导出Excel
	if(!empty($_POST['export'])){
		$export = $_POST['export'];
		$pro = empty($_POST['project']) ? '' : $_POST['project'];
			$sql = "SELECT
			m.company_name,
			m.company_data,
			p.nphone,
			p.project,
			p.lasttime,
			p.units,
			p.currency,
			p.number,
			p.material,
			p.purchase,
			p.plant,
			p.extranum,
			b.price,
			b.btype,
			b.bsize,
			b.delivery,
			b.MOQ,
			b.MPQ,
			b.createtime,
			b.rate
		FROM
			t_member AS m,
			t_product AS p,
			t_bid AS b
		WHERE
			(
				p.mifeng = 0 
				AND p.project = '".$pro."'
				AND b.mid = m.id
				AND p.type = b.btype
			)
		OR (
			p.mifeng = 1
			AND p.lasttime < ".$time."
			AND p.project = '".$pro."'
			AND b.mid = m.id
			AND p.type = b.btype
		)	
		ORDER BY p.type asc, b.price asc
		";
			// echo $sql;

        error_log("table2 SQL4: ".$sql);
		$infoAll = $db_Obj->getall($sql);

	}

	//导出excel
  	if ($export == 1) {
      
       
        $columns = array(
        	array(
                'title' => '序号',
                'field' => 'xuhao',
                'width' => 20
            ),
        	array(
                'title' => '项目',
                'field' => 'project',
                'width' => 20
            ),
        	array(
                'title' => '物料编码',
                'field' => 'btype',
                'width' => 20
            ),
            array(
                'title' => '规格型号',
                'field' => 'bsize',
                'width' => 20
            ),
            array(
                'title' => '供应商',
                'field' => 'company_name',
                'width' => 20
            ),
            array(
                'title' => '供应商代码',
                'field' => 'company_data',
                'width' => 20
            ),
            array(
                'title' => '数量',
                'field' => 'bnumber',
                'width' => 20
            ),
            array(
                'title' => '计量单位',
                'field' => 'units',
                'width' => 20
            ),
            array(
                'title' => '未税价位',
                'field' => 'price',
                'width' => 20
            ),
            array(
                'title' => '税率(%)',
                'field' => 'rate',
                'width' => 20
            ),
            array(
                'title' => '币种',
                'field' => 'currency',
                'width' => 20
            ),
            array(
                'title' => '交期(天)',
                'field' => 'delivery',
                'width' => 20
            ),
            array(
                'title' => 'MOQ(最小订单)',
                'field' => 'MOQ',
                'width' => 20
            ),
            array(
                'title' => 'MPQ(最小包装)',
                'field' => 'MPQ',
                'width' => 20
            ),
            array(
                'title' => '报价截止日期',
                'field' => 'lasttime',
                'width' => 20
            ),
            array(
                'title' => '报价日期',
                'field' => 'createtime',
                'width' =>20
            ),
            array(
                'title' => '物料组',
                'field' => 'material',
                'width' => 20
            ),
            array(
                'title' => '采购组',
                'field' => 'purchase',
                'width' => 20
            ),
            array(
                'title' => '工厂组',
                'field' => 'plant',
                'width' => 20
            ),
            array(
                'title' => '额数',
                'field' => 'extranum',
                'width' => 20
            )
        );
        
        foreach ($infoAll as $key => $value) {
        	$exportlist['xuhao'] =$key+1;
        	$exportlist['project'] =$value['project'];
        	$exportlist['btype'] =$value['btype'];
	        $exportlist['bsize'] =$value['bsize'];
	        $exportlist['company_name'] =$value['company_name'];
	        $exportlist['company_data'] =$value['company_data'];
	        $exportlist['bnumber'] =$value['number'];
	        $exportlist['units'] =$value['units'];
	        $exportlist['price'] =$value['price'];
	        $exportlist['rate'] =$value['rate'];
	        $exportlist['currency'] =$value['currency'];
	        $exportlist['delivery'] =$value['delivery'];
	        $exportlist['MOQ'] =$value['MOQ'];
	        $exportlist['MPQ'] =$value['MPQ'];
	        $exportlist['lasttime'] = date('Y-m-d H:i:s',$value['lasttime']);
	        $exportlist['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
	        $exportlist['material'] =$value['material'];
	        $exportlist['purchase'] =$value['purchase'];
	        $exportlist['plant'] =$value['plant'];
	        $exportlist['extranum'] =$value['extranum'];
	        $arr[] = $exportlist;
        }
       $excObj->export($arr, array(
            "title" => "数据-" . date('Y-m-d-H-i', time()),
            "columns" => $columns
        ));
    }


include 'table2.html';
?>